﻿/// <reference path="jquery-2.1.0.min.js" />
/// <reference path="knockout-3.1.0.js" />
/// <reference path="knockout.mapping-latest.js" />
/// <reference path="jquery.blockUI.min.js" />
/// <reference path="jquery.cookie.js" />
var pccats = [1, 2, 3, 4, 5, 9, 10, 11, 12, 13, 14];

var DesktopBuilderSelectedModel = {
    cpuId: ko.observable(0),
    mainboardId: ko.observable(0),
    ramId: ko.observable(0),
    hddId: ko.observable(0),
    hdd2Id: ko.observable(0),
    monitorId: ko.observable(0),
    vgaId: ko.observable(0),
    cdId: ko.observable(0),
    powerId: ko.observable(0),
    caseId: ko.observable(0),
    keyboardId: ko.observable(0),
    mouseId: ko.observable(0)
};

function DesktopBuilderModel() {
    var self = this;
    var totalPrice = 0;

    self.selectedModel = ko.observable(DesktopBuilderSelectedModel);
    
    //else
    //    self.selectedModel = ko.observable(DesktopBuilderSelectedModel);
    
    self.cpus = ko.observableArray();
    self.mainboards = ko.observableArray();
    self.rams = ko.observableArray();
    self.hdds = ko.observableArray();
    self.hdd2s = ko.observableArray();
    self.monitors = ko.observableArray();
    self.vgas = ko.observableArray();
    self.cds = ko.observableArray();
    self.powers = ko.observableArray();
    self.cases = ko.observableArray();
    self.keyboards = ko.observableArray();
    self.mouses = ko.observableArray();

    self.totalAmount = ko.observable(0);
    //self.selectedCpu = ko.observable();
    
    self.selectedModel().cpuId.subscribe(function (newId) {
        var p = FindObject(self.cpus(), newId);
        totalPrice += p ? p.Price : 0;
        self.totalAmount(totalPrice);
    });
    self.selectedModel().cpuId.subscribe(function (oldId) {
        var p = FindObject(self.cpus(), oldId);
        totalPrice -= p ? p.Price : 0;
        self.totalAmount(totalPrice);
    }, null, 'beforeChange');
    self.selectedModel().mainboardId.subscribe(function (newId) { var p = FindObject(self.mainboards(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().mainboardId.subscribe(function (oldId) { var p = FindObject(self.mainboards(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    self.selectedModel().ramId.subscribe(function (newId) { var p = FindObject(self.rams(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().ramId.subscribe(function (oldId) { var p = FindObject(self.rams(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    self.selectedModel().vgaId.subscribe(function (newId) { var p = FindObject(self.vgas(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().vgaId.subscribe(function (oldId) { var p = FindObject(self.vgas(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    self.selectedModel().cdId.subscribe(function (newId) { var p = FindObject(self.cds(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().cdId.subscribe(function (oldId) { var p = FindObject(self.cds(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    self.selectedModel().powerId.subscribe(function (newId) { var p = FindObject(self.powers(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().powerId.subscribe(function (oldId) { var p = FindObject(self.powers(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    self.selectedModel().hddId.subscribe(function (newId) { var p = FindObject(self.hdds(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().hddId.subscribe(function (oldId) { var p = FindObject(self.hdds(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    self.selectedModel().hdd2Id.subscribe(function (newId) { var p = FindObject(self.hdd2s(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().hdd2Id.subscribe(function (oldId) { var p = FindObject(self.hdd2s(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    self.selectedModel().caseId.subscribe(function (newId) { var p = FindObject(self.cases(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().caseId.subscribe(function (oldId) { var p = FindObject(self.cases(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    self.selectedModel().monitorId.subscribe(function (newId) { var p = FindObject(self.monitors(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().monitorId.subscribe(function (oldId) { var p = FindObject(self.monitors(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    self.selectedModel().keyboardId.subscribe(function (newId) { var p = FindObject(self.keyboards(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().keyboardId.subscribe(function (oldId) { var p = FindObject(self.keyboards(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    self.selectedModel().mouseId.subscribe(function (newId) { var p = FindObject(self.mouses(), newId); totalPrice += p ? p.Price : 0; self.totalAmount(totalPrice); });
    self.selectedModel().mouseId.subscribe(function (oldId) { var p = FindObject(self.mouses(), oldId); totalPrice -= p ? p.Price : 0; self.totalAmount(totalPrice); }, null, 'beforeChange');
    
    $.blockUI();

    // load products
    $.each(pccats, function (index, value) {
        $.ajax({
            url: "Services/ProductService.aspx/PiC",
            type: "post",
            contentType: "application/json",
            data: JSON.stringify({ cId: value }),
            async: true,
            success: function (data) {
                switch (value) {
                    case 1: self.mainboards(data.d); break;
                    case 2: self.cpus(data.d); break;
                    case 3: self.hdds(data.d);
                        var hdd2s = data.d.slice(0);
                        hdd2s.splice(0, 0, { Id: 0, Name: "NONE", NamePrice: "NONE", Price: 0.0 });
                        self.hdd2s(hdd2s);
                        break;
                    case 4: self.rams(data.d); break;
                    case 5: self.monitors(data.d); break;
                    case 9: self.vgas(data.d); break;
                    case 10: self.cds(data.d); break;
                    case 11: self.powers(data.d); break;
                    case 12: self.cases(data.d); break;
                    case 13: self.keyboards(data.d); break;
                    case 14: self.mouses(data.d); break;
                }
                //self.selectedModel().cpuId(5);
                var savedDesktopModel = $.cookie("desktop-model");
                if (savedDesktopModel) {
                    var desktopModel = JSON.parse(savedDesktopModel);
                    self.selectedModel().cpuId(desktopModel.cpuId);
                    self.selectedModel().mainboardId(desktopModel.mainboardId);
                    self.selectedModel().ramId(desktopModel.ramId);
                    self.selectedModel().vgaId(desktopModel.vgaId);
                    self.selectedModel().cdId(desktopModel.cdId);
                    self.selectedModel().powerId(desktopModel.powerId);
                    self.selectedModel().hddId(desktopModel.hddId);
                    self.selectedModel().hdd2Id(desktopModel.hdd2Id);
                    self.selectedModel().caseId(desktopModel.caseId);
                    self.selectedModel().monitorId(desktopModel.monitorId);
                    self.selectedModel().keyboardId(desktopModel.keyboardId);
                    self.selectedModel().mouseId(desktopModel.mouseId);
                }
            }
        });
    });

    // actions
    self.proceed = function () {
        // save cookie selectedModel
        $.cookie("desktop-model", JSON.stringify(ko.toJS(self.selectedModel())));
        // post to remember selected ids
        $.blockUI();
        $.ajax({
            url: "Services/ProductService.aspx/Save",
            type: "post",
            contentType: "application/json",
            data: JSON.stringify({ desktopModel: ko.toJS(self.selectedModel) }),
            async: true,
            success: function (data) {
                // redirect to result page
                window.location = "Results.aspx";
            }
        });
    };
}

// helpers
function FindObject(arr, id) {
    var obj;
    $.each(arr, function (index, object) {
        if (object.Id == id) {
            obj = object;
            return false;
        }
    });
    return obj;
}

function formatCurrency(value) {
    //return value.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') + " VND";
    return value.format() + " VND";
}

Number.prototype.format = function (n, x) {
    var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')';
    return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&,');
};